(function(){
    app.controller('virtualCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','webSocket',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,webSocket) {
            $scope.param = {
                rows: 10
            };
            $scope.showOpenstackDetail = $scope.showVmwareDetail = false;
            var judgeOperate = function (data) {
                switch (data.status) {
                    case 'STOPPING':
                    case 'STARTING':
                    case 'SUSPENDING':
                    case 'RESTARTING':
                    case 'EXCEPTION':
                    case 'UNKNOWNON':
                    case 'ACTIVING':
                    case 'SYNSEXCEPTION':
                    case 'BUILDING':
                        data.disEdit = true;
                        data.disDel = true;
                        data.disPer = true;
                        data.disStart = true;
                        data.disStop = true;
                        data.disRestart = true;
                        data.disSuspended = true;
                        data.disSnap = true;
                        data.disClone = true;
                        data.disMig = true;
                        data.statusColor = 'default';
                        break;
                    case 'RUNNING':
                        data.disDel = true;
                        data.disStart = true;
                        data.statusColor = 'success';
                        break;
                    case 'STOPPED':
                        data.disPer = true;
                        data.disStop = true;
                        data.disRestart = true;
                        data.disSuspended = true;
                        data.statusColor = 'warning';
                        break;
                    case 'SUSPENDED':
                        data.disEdit = true;
                        data.disRestart = true;
                        data.disSuspended = true;
                        data.statusColor = 'primary';
                        break;
                    case 'UNKNOWNOFF':
                    case 'UNKNOWNSUSPEND':
                        data.disEdit = true;
                        data.disDel = true;
                        data.disStart = true;
                        data.disStop = true;
                        data.disRestart = true;
                        data.disSuspended = true;
                        data.disPer = true;
                        data.disSnap = true;
                        data.disClone = true;
                        data.statusColor = 'default';
                        break;
                }
            };
            //获取云主机列表
            $scope.getData = function (page) {
                $scope.isBatch = true;$scope.selectAll = false;$scope.showDetail = false;
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows
                    },
                    searchParam = [{"param": {"isTemplate": false}, "sign": "EQ"}];
                if ($scope.searchByName && $scope.searchByName != "") {
                    searchParam.push({"param": {"name": $scope.searchByName}, "sign": "LK"});
                }
                if ($scope.searchByStatus && $scope.searchByStatus != "") {
                    var a = 0;
                    for (var i = 0; i < searchParam.length; i++) {
                        if (searchParam[i].sign == "LK") {
                            a = 1;
                            searchParam[i].param.status = $scope.searchByStatus;
                        }
                    }
                    if (a == 0) searchParam.push({"param": {"status": $scope.searchByStatus}, "sign": "LK"});
                }
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url: '/vm/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success: function (data) {
                        if (data.success && data.data.rows) {
                            $scope.vmList = data.data.rows;
                            $scope.totalCount = data.data.total;
                            if(data.data.rows.length==0) {
                                $scope.isImageData = true;
                                return;
                            } else $scope.isImageData = false;
                            $scope.vmList.forEach(function (data, index) {
                                if(data.vendorType == 'VMWARE') {
                                    judgeOperate(data);
                                    return;
                                }
                                data.isSelect = false;
                                //状态
                                data.isShowRecover = data.isRecover = data.isShowPaused = data.isPaused = data.isShowSuspended = data.isSuspended = data.isShowActive = data.isActive = data.isShowStart = data.isStart = data.isShowStop = data.isStop = false;
                                data.isOffline = true;
                                switch (data.status) {
                                    case 'RUNNING':
                                        data.isShowStop = true;data.isStop = false;data.isShowRestart = true;data.isRestart = false;
                                        data.isShowStart = false;data.isShowPaused = true;data.isPaused = false;data.isShowSuspended = true;data.isSuspended = false;data.statusColor = 'success';
                                        break;
                                    case 'STOPPED':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;data.isStart = false;
                                        data.isShowRecover = true;data.isRecover = false;data.isShowActive = true;data.isActive = false;data.statusColor = 'warning';
                                        data.isOffline = false;
                                        break;
                                    case 'STOPPING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;
                                        data.isStart = true;data.isShowRecover = true;data.isRecover = true;data.isShowActive = true;data.isActive = true;data.statusColor = 'default';
                                        break;
                                    case 'STARTING':
                                        data.isShowStop = true;data.isStop = true;data.isShowRestart = true;data.isRestart = false;
                                        data.isShowStart = false;data.isShowPaused = true;data.isPaused = true;data.isShowSuspended = true;data.isSuspended = true;data.statusColor = 'default';
                                        break;
                                    case 'SUSPENDED':
                                        if(data.vendorType == 'VMWARE'){
                                            data.isShowStop = true;
                                            data.isShowRestart = false;
                                            data.isRestart = false;
                                            data.isShowStart = true;
                                            data.isShowActive = true;
                                            data.isActive = false;
                                        }else{
                                            data.isShowStop = false;
                                            data.isShowRestart = true;data.isRestart = true;
                                            data.isShowStart = false;
                                            data.isShowActive = true;data.isActive = false;
                                            data.isException = true;
                                        }
                                        data.statusColor = 'primary';
                                        break;
                                    case 'SUSPENDING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = false;
                                        data.isShowActive = true;data.isActive = true;data.statusColor = 'default';
                                        break;
                                    case 'ACTIVING':
                                        data.isShowStop = true;data.isStop = true;data.isShowRestart = true;data.isRestart = false;data.isShowStart = false;
                                        data.isShowPaused = true;data.isPaused = true;data.isShowSuspended = true;data.isSuspended = true;data.statusColor = 'default';
                                        break;
                                    case 'PAUSED':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;
                                        data.isShowStart = false;data.isShowRecover = true;data.isRecover = false;data.statusColor = 'warning';
                                        break;
                                    case 'PAUSING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;
                                        data.isShowStart = false;data.isShowRecover = true;data.isRecover = true;data.statusColor = 'default';
                                        break;
                                    case 'RECOVERING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = false;
                                        data.isShowPaused = true;data.isPaused = true;data.isShowSuspended = true;data.isSuspended = true;data.statusColor = 'default';
                                        break;
                                    case 'PAUSING':
                                        data.isShowStop = true;data.isStop = true;
                                        data.isShowRestart = true;data.isRestart = false;data.isShowStart = false;data.statusColor = 'default';
                                        break;
                                    case 'BUILDING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;data.isStart = true;
                                        data.isShowRecover = true;data.isRecover = true;data.isShowActive = true;data.isActive = true;data.statusColor = 'default';
                                        break;
                                    case 'RESTARTING':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = false;
                                        data.isShowPaused = true;data.isPaused = true;data.isShowSuspended = true;data.isSuspended = true;data.statusColor = 'default';
                                        break;
                                    case 'EXPIRED':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;data.isStart = true;
                                        data.isShowRecover = true;data.isRecover = true;data.isShowActive = true;data.isActive = true;data.statusColor = 'default';
                                        break;
                                    case 'EXCEPTION':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;data.isStart = true;
                                        data.isShowRecover = true;data.isRecover = true;data.isShowActive = true;data.isActive = true;data.statusColor = 'danger';
                                        break;
                                    case 'SYNSEXCEPTION':
                                        data.isShowStop = false;data.isShowRestart = true;data.isRestart = true;data.isShowStart = true;
                                        data.isStart = true;data.isShowRecover = true;data.isRecover = true;data.isShowActive = true;data.isActive = true;data.statusColor = 'danger';
                                        break;
                                }
                            });
                        } else {
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            //websocket异步操作
            webSocket.onmessage({
                message:function (data) {
                    if($rootScope.currentUrl=='app.assets.resource'&&$scope.active2&&data.operate.indexOf('vm')>-1){
                        $scope.pop(data.message,data.success?'success':'error');
                        $scope.getData();
                    }
                }
            });
            //如果Tab是当前页
            var resourcetabLocation = sessionStorage.getItem('resourcetabLocation');
            if(resourcetabLocation&&resourcetabLocation.indexOf("virtual")>=0) $scope.getData(1);
            //状态数据
            $scope.statusData = [{"value": "RUNNING", "name": "运行中"}, {
                "value": "STOPPED",
                "name": "关机"
            }, {"value": "SUSPENDED", "name": "挂起"}, {"value": "PAUSED", "name": "停止"}, {"value": "EXCEPTION", "name": "异常"}];

            //重置
            $scope.reset = function(){
                $scope.searchByName = "";$scope.searchByStatus = "";
            };
            $scope.isBatch = true;$scope.selectAll = false;
            //批量操作
            $scope.chooseAll = function(){
                $scope.isBatch = !$scope.selectAll;
                $scope.vmList.forEach(function(item){
                    item.isSelect = $scope.selectAll;
                });
            };
            $scope.choose = function(){
                var a = 0,b=0;
                $scope.vmList.forEach(function(item){
                    if(item.isSelect==true) a++;
                    else b=1;
                });
                if(a>=1) $scope.isBatch = false;
                else $scope.isBatch = true;
                if(b==1) $scope.selectAll = false;
                else $scope.selectAll = true;
            };
            $scope.batch = function($event,value){
                $event.stopPropagation();
                $scope.operationData = '';var operationData = '';
                $scope.vmList.forEach(function(item){
                    if(item.isSelect==true) operationData += ','+item.id;
                });
                $scope.operationData = operationData.slice(1);
                $scope.remove($event,{'id':$scope.operationData,'status':'','batchUrl':1},value);
            };

            //返回
            $scope.goBack = function(){
                $scope.isActive = false;
                $timeout(function() {
                    $scope.showOpenstackDetail = $scope.showVmwareDetail = false;
                }, 200);
            };
            $scope.paramd = {
                page:1,
                rows:10
            };
            //详情快照列表
            $scope.detail = {
                snapParam:{
                    page:1,
                    rows:10
                },
                getSnapshot:function (page,id) {
                    $scope.detail.snapParam.page = page || $scope.detail.snapParam.page;
                    $scope.detail.snapParam.params = JSON.stringify([{"param": {"vmId": id || $scope.vmDetail.id}, "sign": "EQ"}]);
                    httpLoad.loadData({
                        url: '/snapshot/list',
                        method: 'POST',
                        data: $scope.detail.snapParam,
                        noParam: true,
                        success: function (data) {
                            if (data.success) {
                                $scope.detail.snapshotList = data.data.rows;
                                $scope.detail.totalSnap = data.data.total;
                                if (data.data.rows.length == 0) {
                                    $scope.detail.noneSnap = true;
                                    return;
                                } else $scope.detail.noneSnap = false;
                                angular.forEach($scope.detail.snapshotList, function (data, index) {
                                    if (data.status == 'AVAILABLE') data.status1 = '正常';
                                    else if (data.status == 'INAVAILABLE') data.status1 = '不正常';
                                });
                            } else {
                                $scope.detail.noneSnap = true;
                            }
                        }
                    });
                }
            };
            //详情操作记录
            $scope.getOperateData = function(page,id){
                $scope.getOperateDataId = id||$scope.getOperateDataId;
                $scope.paramd.page = page || $scope.paramd.page;
                var params = {
                        page: $scope.paramd.page,
                        rows: $scope.paramd.rows
                    },
                    searchParam = [];
                searchParam.push({"param":{"vmId":$scope.getOperateDataId},"sign":"EQ"});
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/res/event/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            if(data.data){
                                $scope.eventList = data.data.rows;
                                $scope.totalCountd = data.data.total;
                                $scope.isImageDatad = false;
                            }
                        }else{
                            $scope.isImageDatad = true;
                        }
                    }
                });
            };
            //跳转详情页
            $scope.goDetail = function ($event,row) {
                $event.stopPropagation();
                httpLoad.loadData({
                    url:'/vm/detail',
                    method:'GET',
                    data: {id: row.id},
                    success:function(data){
                        if(data.success&&data.data){
                            $scope.vmDetail = data.data;
                            switch(row.vendorType){
                                case 'OPENSTACK':
                                    $scope.showOpenstackDetail = true;$scope.showVmwareDetail = false;
                                    break;
                                case 'VMWARE':
                                    $scope.showOpenstackDetail = false;$scope.showVmwareDetail = true;
                                    break;
                            }
                            $scope.isActive = true;
                            $scope.showOpenstackDetail = true;
                            //状态处理
                            switch($scope.vmDetail.status){
                                case 'RUNNING':
                                    $scope.vmDetail.statusColor = "success";
                                    break;
                                case 'STOPPED':
                                    $scope.vmDetail.statusColor = "warning";
                                    break;
                                case 'STOPPING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'STARTING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'SUSPENDED':
                                    $scope.vmDetail.statusColor = "primary";
                                    break;
                                case 'SUSPENDING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'ACTIVING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'PAUSED':
                                    $scope.vmDetail.statusColor = "warning";
                                    break;
                                case 'PAUSING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'RECOVERING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'PAUSING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'BUILDING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'RESTARTING':
                                    $scope.vmDetail.statusColor = "default";
                                    break;
                                case 'EXCEPTION':
                                    $scope.vmDetail.statusColor = "danger";
                                    break;
                                case 'SYNSEXCEPTION':
                                    $scope.vmDetail.statusColor = "danger";
                                    break;
                            }
                            //密码处理
                            var password = '';
                            if($scope.vmDetail.password){
                                for(var i=0;i<$scope.vmDetail.password.length;i++){
                                    password +='*';
                                }
                                $scope.vmDetail.password = password;
                            }
                        }
                    }
                });
                //获取列表数据
                $scope.getOperateData(1,row.id);
                if(row.vendorType=='VMWARE') $scope.detail.getSnapshot(1,row.id);
            };
            //新增
            $scope.add = function ($event) {
                $event.stopPropagation();
                $state.go('app.assets.addResource');
            };
            //编辑
            $scope.update = function ($event,row) {
                switch(row.vendorType){
                    case 'OPENSTACK':
                        $state.go('app.config.openstackUpdate', {id:row.vendorId,vmId:row.id});
                        sessionStorage.setItem('resourcetabLocation1', JSON.stringify('virtualUpdate'));
                        break;
                    case 'VMWARE':
                        $state.go('app.config.vmwareUpdate', {id: row.id});
                        sessionStorage.setItem('vmTabvendor', JSON.stringify({id:row.vendorId}));
                        sessionStorage.setItem('resourcetabLocation1', JSON.stringify('virtualUpdate'));
                        break;
                }
            };
            //克隆
            $scope.clone = function (id,vendorId) {
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/config/vmware/vm/cloneHostModal.html',
                    controller: 'cloneHostModalCtrl1',
                    backdrop: 'static',
                    resolve: {
                        id: function () {
                            return id;
                        },
                        vendorId:function(){
                            return vendorId;
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.getData();
                });
            };
            //热迁移(vmware)
            $scope.migrate = function (id,vendorId) {
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/config/vmware/vm/migrateModal.html',
                    controller: 'migrateModalCtrl1',
                    backdrop: 'static',
                    resolve: {
                        id: function () {
                            return id;
                        },
                        vendorId:function(){
                            return vendorId;
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.getData();
                });
            };
            //创建快照
            $scope.createSnapshot = function (id,vendorId) {
                var modalInstance = $modal.open({
                    templateUrl: '/statics/tpl/config/vmware/vm/addSnapshotModal.html',
                    controller: 'addSnapshotVModalCtrl',
                    backdrop: 'static',
                    resolve: {
                        id: function () {
                            return id;
                        },
                        vendorId: function () {
                            return vendorId;
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.getData();
                });
            };
            //新增快照(openstack)
            $scope.addSnapshot = function($event,id,isAction,vendorId){
                $event.stopPropagation();
                if(isAction==true) return;
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/config/openstack/vm/addSnapshot2.html',
                    controller : 'addopenstackSnapshotModalCtrl3',
                    resolve : {
                        id : function(){
                            return id;
                        },
                        vendorId:vendorId
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            $scope.catPerformance = function (name,vendorId,action) {
                var obj = {
                    flag:2,
                    name:name,
                    vendorId:vendorId
                };
                sessionStorage.setItem('performance',JSON.stringify(obj))
                if(action==1) $state.go('app.config.openstackPerformance');
                if(action==2) $state.go('app.config.vmperformance');
            };
            //在线迁移(openstack热迁移)
            $scope.onlineTransfer = function($event,row,isAction){
                $event.stopPropagation();
                if(isAction==true) return;
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/config/openstack/vm/onlinetransfer.html',
                    controller : 'onlinetransferOpenstackModalCtrl1',
                    resolve : {
                        id : function(){
                            return row.id;
                        },
                        name:function(){
                            return row.name;
                        },
                        vendorId:function(){
                            return row.vendorId;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //离线迁移（openstack冷迁移）
            $scope.offlineTransfer = function($event,row,isAction){
                $event.stopPropagation();
                if(isAction==true) return;
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/config/openstack/vm/offlinetransfer.html',
                    controller : 'offlinetransferOpenstackModalCtrl1',
                    resolve : {
                        id : function(){
                            return row.id;
                        },
                        name:function(){
                            return row.name;
                        },
                        vendorId:function(){
                            return row.vendorId;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //删除6,备份3，快照4，恢复0，暂停7，挂起8，激活9，重启1，开机2，关机5
            $scope.remove = function($event,row,value,isAction){
                if($event) $event.stopPropagation();
                if(isAction==true) return;
                var txt = '';
                if(value==6) {
                    if(row.status!='STOPPED'){
                        txt = '虚机正在运行中，无法删除';
                    }
                }
                $scope.removeData= {"id":row.id};
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/config/openstack/vm/remove.html',
                    controller : 'removeOpenstackModalCtrl1',
                    resolve : {
                        removeData : function(){
                            return $scope.removeData;
                        },
                        value: function(){
                            return value;
                        },
                        txt:function(){
                            return txt;
                        },
                        batchUrl:function(){   //批量操作才有
                            return row.batchUrl;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
        }
    ]);
    //虚机克隆
    app.controller('cloneHostModalCtrl1', ['$rootScope','$scope', '$modalInstance', '$stateParams', 'httpLoad', 'id','vendorId',
        function ($rootScope,$scope, $modalInstance, $stateParams, httpLoad, id,vendorId) {
            $rootScope.link = '/statics/css/user.css';
            $scope.addData = {
                id:id
            };
            //获取数据存储数据
            var getDatastore = function(vhostId){
                var params = {
                        simple : true
                    },
                    searchParam = [{"param":{"vendorId":vendorId,"vhostId":vhostId},"sign":"EQ"}];
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/datastore/list',
                    method: 'POST',
                    data:params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            $scope.datastoreData = data.data.rows;
                        }
                    }
                });
            };
            //获取目标主机数据
            var getVhost = function(id,from){
                var searchParam = [];
                if(from=='vdc'){
                    searchParam = [{"param":{"vendorId":vendorId,"vdcId":id},"sign":"EQ"}]
                }else if(from=='cluster'){
                    searchParam = [{"param":{"vendorId":vendorId,"clusterId":id},"sign":"EQ"}]
                }
                var params = {
                    page:1,
                    rows:10000
                };
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/vhost/list',
                    method: 'POST',
                    data:params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            $scope.vhostData = data.data.rows;
                        }
                    }
                });
            };
            //获取集群数据
            var getCluster = function(vdcId){
                var params = {
                        simple : true
                    },
                    searchParam = [{"param":{"vendorId":vendorId,"vdcId":vdcId},"sign":"EQ"}];
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/cluster/list',
                    method: 'POST',
                    data:params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            $scope.clusterData = data.data.rows;
                        }
                    }
                });
            };
            //获取数据中心数据
            var getVdc = function(){
                var params = {
                        simple : true
                    },
                    searchParam = [{"param":{"vendorId":vendorId},"sign":"EQ"}];
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/vdc/list',
                    method: 'POST',
                    data:params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            $scope.vdcData = data.data.rows;
                        }
                    }
                });
            };
            getVdc();
            //选择数据中心
            $scope.chooseVdc = function(item){
                $scope.vdcData.forEach(function(data){
                    data.isVdcActive = false;
                    if(data.id==item.id){
                        data.isVdcActive = !item.isVdcActive;
                    }
                    if(data.isVdcActive){
                        $scope.vdcId = data.id;
                        $scope.clusterId = '';$scope.addData.hostname = '';$scope.addData.datastore = '';$scope.datastoreData = '';
                        getCluster($scope.vdcId);
                        getVhost($scope.vdcId,'vdc');
                        return;
                    }
                });
            };
            //选择集群
            $scope.chooseCluster = function(item){
                $scope.clusterData.forEach(function(data){
                    data.isClusterActive = false;
                    if(data.id==item.id){
                        data.isClusterActive = !item.isClusterActive;
                    }
                    if(data.isClusterActive){
                        $scope.clusterId = data.id;
                        $scope.addData.hostname = '';$scope.addData.datastore = '';$scope.datastoreData = '';
                        getVhost($scope.clusterId,'cluster');
                        return;
                    }
                });
            };
            //选择目标主机
            $scope.chooseVhost = function(item){
                if(!item.connected || item.maintain) return $scope.pop('无法选择该虚拟机，因为该虚拟机未连接、处于维护模式下，或者在更改其维护状态','info')
                $scope.vhostData.forEach(function(data){
                    data.isVhostActive = false;
                    if(data.id==item.id){
                        data.isVhostActive = !item.isVhostActive;
                    }
                    if(data.isVhostActive){
                        $scope.addData.hostname = data.name;
                        $scope.addData.hostId = data.id;
                        $scope.addData.datastore = '';$scope.datastoreData = '';
                        getDatastore(data.id);
                        return;
                    }
                });
            };
            //选择数据存储
            $scope.chooseDatastore = function(item){
                $scope.datastoreData.forEach(function(data){
                    data.isDatastoreActive = false;
                    if(data.id==item.id){
                        data.isDatastoreActive = !item.isDatastoreActive;
                    }
                    if(data.isDatastoreActive){
                        $scope.addData.datastore = data.name;
                        $scope.addData.storeId = data.id;
                        return;
                    }
                });
            };

            $scope.ok = function () {
                if(!$scope.addData.hostname&&$scope.addData.hostname==''){
                    $scope.pop('请选择目标主机','error');
                    return;
                }
                if(!$scope.addData.datastore&&$scope.addData.datastore==''){
                    $scope.pop('请选择数据存储','error');
                    return;
                }
                httpLoad.loadData({
                    url: '/vm/clone',
                    data: $scope.addData,
                    success: function (data) {
                        if (data.success) {
                            $scope.pop(data.message);
                            $modalInstance.close(data);
                        }
                    }
                });
            }
            $scope.cancle = function () {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    //虚机热迁移（VMware）
    app.controller('migrateModalCtrl1', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'id','vendorId',
        function ($scope, $modalInstance, $stateParams, httpLoad, id,vendorId) {
            //获取物理机列表
            (function () {
                httpLoad.loadData({
                    url: '/vhost/list',
                    method: 'POST',
                    data:{
                        simple:true,
                        params:JSON.stringify([{"param": {"vendorId": vendorId}, "sign": "EQ"}])
                    },
                    noParam: true,
                    success: function (data) {
                        if (data.success) {
                            $scope.vmHostList = data.data.rows;
                        }
                    }
                });
            })();
            $scope.ok = function () {
                var hostData = JSON.parse($scope.hostData)
                httpLoad.loadData({
                    url: '/vm/migrate',
                    data: {
                        id:id,
                        hostname:hostData.name,
                        hostId:hostData.id
                    },
                    success: function (data) {
                        if (data.success) {
                            $scope.pop(data.message);
                            $modalInstance.close(data);
                        }
                    }
                });
            }
            $scope.cancle = function () {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    //在线迁移ctrl（openstack）
    angular.module('app').controller('onlinetransferOpenstackModalCtrl1',['$rootScope','$scope','$modalInstance','httpLoad','$stateParams','id','name','vendorId',
        function($rootScope,$scope,$modalInstance,httpLoad,$stateParams,id,name,vendorId){
            $rootScope.link = '/statics/css/user.css';
            $scope.syncData = {
                id: vendorId,
                serverId:id,
                diskOverCommit:false,
                blockMigration:false,
                live:true
            };
            $scope.name = name;$scope.hostData = [];
            //获取主机数据
            $scope.getHost = function(){
                var params = {
                        page: 1,
                        rows: 9999
                    },
                    searchParam = [{"param":{"vendorId":vendorId},"sign":"EQ"}];
                params.params = JSON.stringify(searchParam);
                httpLoad.loadData({
                    url:'/vhost/list',
                    method: 'POST',
                    data: params,
                    noParam: true,
                    success:function(data){
                        if(data.success){
                            var dataList = data.data.rows;
                            angular.forEach(dataList, function(data,index){
                                if(data.status=='enabled') $scope.hostData.push(data);
                            });
                        }
                    }
                });
            };
            $scope.getHost();
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/vm/OSMigrate',
                    method:'POST',
                    data: $scope.syncData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //离线迁移ctrl（openstack）
    angular.module('app').controller('offlinetransferOpenstackModalCtrl1',['$rootScope','$scope','$modalInstance','httpLoad','$stateParams','id','name','vendorId',
        function($rootScope,$scope,$modalInstance,httpLoad,$stateParams,id,name,vendorId){
            $rootScope.link = '/statics/css/user.css';
            $scope.syncData = {
                id:vendorId,
                serverId:id,
                live:false
            };
            $scope.name = name;
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/vm/OSMigrate',
                    method:'POST',
                    data: $scope.syncData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除ctrl
    angular.module('app').controller('removeOpenstackModalCtrl1',['$scope','$modalInstance','httpLoad','removeData','value','txt','batchUrl',
        function($scope,$modalInstance,httpLoad,removeData,value,txt,batchUrl){
            var url;
            //删除6,备份3，快照4，恢复0，停止7，挂起8，激活9，重启1，开机2，关机5
            if(value==0){
                $scope.actionName = '恢复';
                url = '/vm/recovery';
            }
            if(value == 1){
                if(batchUrl&&batchUrl==1){
                    $scope.actionName = '批量重启';
                    url = '/vm/batchReboot';
                }else{
                    $scope.actionName = '重启';
                    url = '/vm/restart';
                }
            }
            if(value == 3){
                $scope.actionName = '备份';
                url = '/vm/backup';
            }
            if(value == 4){
                $scope.actionName = '快照';
                url = '/vm/snapshoot';
            }
            if(value == 6){
                if(batchUrl&&batchUrl==1){
                    $scope.actionName = '批量删除';
                    url = '/vm/batchRemove';
                }else{
                    $scope.actionName = '删除';
                    url = '/vm/remove';
                }
            }
            if(value == 7){
                $scope.actionName = '停止';
                url = '/vm/pause';
            }
            if(value == 8){
                $scope.actionName = '挂起';
                url = '/vm/suspend';
            }
            if(value == 9){
                $scope.actionName = '激活';
                url = '/vm/active';
            }
            if(value == 2){
                if(batchUrl&&batchUrl==1){
                    $scope.actionName = '批量开机';
                    url = '/vm/batchStart';
                }else{
                    $scope.actionName = '开机';
                    url = '/vm/start';
                }
            }
            if(value == 5){
                if(batchUrl&&batchUrl==1){
                    $scope.actionName = '批量关机';
                    url = '/vm/batchStop';
                }else{
                    $scope.actionName = '关机';
                    url = '/vm/stop';
                }
            }
            $scope.ok = function(){
                httpLoad.loadData({
                    url:url,
                    method:'POST',
                    data: removeData,
                    success:function(data){
                        if(data.success==true){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            if(value == 6&&txt!='') $scope.pop(data.message,'error');
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
    //虚机创建快照
    app.controller('addSnapshotVModalCtrl', ['$scope', '$modalInstance', '$stateParams', 'httpLoad', 'id','vendorId',
        function ($scope, $modalInstance, $stateParams, httpLoad, id, vendorId) {
            $scope.ok = function () {
                httpLoad.loadData({
                    url: '/vm/snapshot',
                    data: {
                        id: vendorId,
                        serverId: id,
                        name: $scope.name,
                        remark: $scope.remark
                    },
                    success: function (data) {
                        if (data.success) {
                            $scope.pop(data.message);
                            $modalInstance.close(data);
                        }
                    }
                });
            }
            $scope.cancle = function () {
                $modalInstance.dismiss('cancel');
            };
        }
    ]);
    //虚机新增快照(openstack)
    angular.module('app').controller('addopenstackSnapshotModalCtrl3',['$rootScope','$scope','$modalInstance','httpLoad','$stateParams','id','vendorId',
        function($rootScope,$scope,$modalInstance,httpLoad,$stateParams,id,vendorId){ //依赖于modalInstance
            $scope.addData = {
                id:vendorId,
                serverId:id
            };
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/vm/snapshot',
                    method:'POST',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();